from math import *
from numpy import *
import matplotlib.pyplot as plt

def diff_alante(f, x_0, h):
    return (f(x_0+h)-f(x_0))/h

h = 0.15
x_0 = [-2.5, -2.0, -1.5, -1.0]

def funcion(x):
    return x*sqrt(9-x**2) + 9*arcsin(x/3)

def derivada_funcion(x):
    return 3/(sqrt(1-((x**2)/9))) + sqrt(9-x**2) - (x**2)/(sqrt(9-x**2))

# Funcion derivada hacia adelante

derivada_en_x0 = [diff_alante(funcion, i, h) for i in x_0]

print(f"derivada hacia adelante, h = {h}")
for i in x_0:
    print(f"\ten x0 = {i}:\t{diff_alante(funcion, i, h):.5f}")


x = linspace(-2.75, 2.75, 100)
y = derivada_funcion(x)

plt.scatter(x_0, derivada_en_x0,color="red", label="ec.15, h=0.15")
plt.plot(x, y, label="f'(x) exacta")
plt.xlabel("x")
plt.ylabel("df/dx")
plt.legend(loc="upper right")
plt.grid()
plt.show()